package com.qiaomengapp.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qiaomengapp.bean.ProviderGood;
import com.qiaomengapp.bean.ShopGood;
import com.qiaomengapp.bean.ShopGoodWrap;
import com.qiaomengapp.bean.ShopWarp;
import com.qiaomengapp.bean.User;
import com.qiaomengapp.exception.CustomException;
import com.qiaomengapp.service.IProvideGoodService;
import com.qiaomengapp.service.IShopGoodService;
import com.qiaomengapp.service.IShopService;
import com.qiaomengapp.service.IUserFocusService;

@Controller
@RequestMapping("shopGood")
public class ShopGoodController {
	@Resource
	private IShopGoodService service;
	@Resource
	private IProvideGoodService service1;
	@Autowired
	private IUserFocusService iUserFocusService;
	@Autowired
	private IShopService iShopService;
	
	//根据shop_Id显示相应商店上架的商品
	@RequestMapping("manageGood")
	@ResponseBody
	public List<ShopGood> manageGood(){
		try {
			List<ShopGood> list = service.select_ShopGood(1);
			return list;
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return null;
	}
	//根据shop_Id显示相应商店下架的商品
	@RequestMapping("manageGood1")
	@ResponseBody
	public List<ShopGood> manageGood1(HttpSession session){
		//得到userd对象
		User user = (User) session.getAttribute("user");
		if (user==null) {
			user = new User();
			user.setId(1);
		}
		try {
			//根据userid查询shopid
			ShopWarp warp = iShopService.select_shopByUid(user.getId());
			List<ShopGood> list = service.select_ShopGoodStatus2(warp.getId());
			return list;
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return null;
	}
	//根据 销量显示商品
	@RequestMapping("select_ShopGoodBySaleCount")
	@ResponseBody
	public List<ShopGood> select_ShopGoodBySaleCount(){
		try {
			List<ShopGood> list = service.select_ShopGoodBySaleCount(1);
			return list;
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return null;
	}
	//根据 佣金显示商品
	@RequestMapping("select_ShopGoodByCommission")
	@ResponseBody
	public List<ShopGood> select_ShopGoodByCommission(){
		try {
			List<ShopGood> list = service.select_ShopGoodByCommission(1);
			return list;
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return null;
	}
	//根据 售价显示商品
	@RequestMapping("select_ShopGoodByPrice")
	@ResponseBody
	public List<ShopGood> select_ShopGoodByPrice(){
		try {
			List<ShopGood> list = service.select_ShopGoodByPrice(1);
			return list;
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return null;
	}
	//根据 收藏数显示商品
	@RequestMapping("select_ShopGoodByfocus_num")
	@ResponseBody
	public List<ShopGood> select_ShopGoodByfocus_num(){
		try {
			List<ShopGood> list = service.select_ShopGoodByfocus_num(1);
			return list;
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return null;
	}
	
	//上架
	@RequestMapping("deleteGood")
	@ResponseBody
	public void deleteGood(int[] shopGoodId){
		try {
			for (int i : shopGoodId) {
				service.delete_shopGood(i);
			}
			
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
	}
	
	//下架
	@RequestMapping("changeStatusTo2")
	@ResponseBody
	public void changeStatusTo2(int[] shopGoodId){
		for (int i : shopGoodId) {
			try {
				service.update_statusTo2(i);
			} catch (CustomException e) {
				// TODO Auto-generated catch block
				System.out.println(e.getMessage());
			}
		}
	}
	
	//给商店添加关注
		@RequestMapping("addcollectNum")
		@ResponseBody
		public void addcollectNum(Integer shopId,HttpSession session){
			//登录用户
			User user = (User) session.getAttribute("user");
			if (user==null) {
				user = new User();
				user.setId(1);
			}
			try {
				service.update_shop_addcollectNum(shopId);
				iUserFocusService.insert_shop(user.getId(), shopId);
			} catch (CustomException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	//店家添加商品，在shop_good表中增加一条或多条数据
		@RequestMapping("addGood")
		@ResponseBody
		public int addGood(int [] provideGoodId){
			//从session中取shop_id,根据privide_good_id从数据库获取相应的供应商商品信息
			int k=0;
			for (int i : provideGoodId) {
				k++;
				ProviderGood pro;
				try {
					pro = service1.select_ProviderGoodbyId(i);
					service.insert_Good(pro, 1);
				} catch (CustomException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return k;
		}
		
	//根据shop_good_id显示商品的具体信息
		@RequestMapping("showHtml")
		@ResponseBody	
		public ShopGoodWrap showHtml(Integer shopGoodId){
			if (shopGoodId==null) {
				shopGoodId=1;
			}
			try {
				ShopGoodWrap wrap = service.select_ShopGoodWrap(shopGoodId);
				return wrap;
			} catch (CustomException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
}
